﻿using HelloWPF.DependencyProperty.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HelloWPF.DependencyProperty
{
    /// <summary>
    /// DependencePropertyStudentDemo.xaml 的交互逻辑
    /// </summary>
    public partial class DependencePropertyStudent : Window
    {
        Student student = null;

        public DependencePropertyStudent()
        {
            InitializeComponent();

            #region 让textBox1作为数据源，把Student实例作为数据的目标，让Student实例依赖在textBox1上
            {
                student = new Student();
                Binding binding = new Binding("Text") { Source = this.textBox1 };
                BindingOperations.SetBinding(student, Student.NameProperty, binding);
                this.textBox2.SetBinding(TextBox.TextProperty, binding);
            }
            #endregion
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            #region 尝试用依赖属性来存储值并把值读取出来
            //{
            //    student = new Student();
            //    student.SetValue(Student.NameProperty, this.textBox1.Text);
            //    MessageBox.Show(student.GetValue(Student.NameProperty).ToString());
            //    this.textBox2.Text = (string)student.GetValue(Student.NameProperty);
            //}
            #endregion

            #region 有了CLR属性包装，我们就可以这样访问依赖属性了
            {
                student = new Student();
                student.Name = this.textBox1.Text;
                this.textBox2.Text = student.Name;
            }
            #endregion
        }
    }
}
